<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Template Library
 *
 * Parses view files before they are displayed
 *
 * @package		SimplyPost
 * @subpackage	Libraries
 * @author		Pascal Kriete
 */

class Template {

    var $item = array();
    
	/**
	 * Constructor
	 *
	 * @access	public
	 */	
    function Template()
	{
        $this->CI =& get_instance();
        log_message('debug', 'Template class initialized.');

		//Let's keep the constructor clean - load our standard tags...
		$this->_init();
    }

	// --------------------------------------------------------------------

	/**
	 * Accessor to add a template
	 *
	 * @access	public
	 * @param	string	tag name
	 * @param	string	injected before content
	 * @param	string	injected after content
	 * @param	bool	do we display it (such as $this->is_admin)
	 * @return	void
	 */
    function add_tag($tag, $pre = '', $post = '', $cond = TRUE)
	{
		$this->item[$tag] = array($pre, $post, $cond);
    }

	// --------------------------------------------------------------------

	/**
	 * Replace the tags - called by loader
	 *
	 * @access	public
	 * @param	string	text containing the tags
	 * @return	string	text with the tags replaced
	 */
	function replace_tags($text)
	{
		foreach($this->item as $key => $template)
		{
			$find = array('{'.$key.'}', '{/'.$key.'}');
			$start = $template[0];
			$stop = $template[1];
			
			if ($template[2]) {
				$start = '<? if('.$template[2].'): ?>'.$start;
				$stop .= '<? endif; ?>';
			}
			
			$text = str_replace($find, array($start, $stop), $text);
		}
		return $text;
	}
	
	// --------------------------------------------------------------------

	/**
	 * Default tags (use add_tag if you need dynamic ones)
	 *
	 * @access	private
	 * @return	void
	 */
	function _init()
	{
		// Admin Variables (so the array stays readable)
		$title = '<p id="admin_title">Admin Tools <span id="admin_handle">&and;</span></p>';
		$title .= '<noscript><p><strong>Administration actions require JavaScript.</strong></p></noscript>';
		$toggles = '<span class="tog_cap">Inline Editor: </span><span class="on" id="evt_toggle">On</span><br/>';
		$toggles .= '<span class="tog_cap">Confirm Delete: </span><span class="on" id="conf_toggle">On</span><br/>';
		
		// Nag warning
		if (file_exists('./install.php'))
			$title .= '<p><strong>Please remove the installer.</strong></p>';
		
		// Array for key definitions: 'tag' => array( 'pre', 'post', 'bool' )
		$this->item = array(
			'user' => array(
				'',
				'',
				'$this->logged_in'
			),
			'admin' => array(
				'',
				'',
				'$this->is_admin'
			),
			'not_admin' => array(
				'',
				'',
				'!$this->is_admin'
			),
			'admin_box' => array(
				'<div id="admin">'.$title.'<div id="admin_body" class="hide hidden">',
				'<div id="toggles">'.$toggles.'</div></div></div>',
				'$this->is_admin'
			),
			'debug' => array(
				'',
				'',
				'DEBUG'
			)
		);
	}
}